package xjh.him.fragment;

import android.app.Fragment;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;
import androidx.core.content.ContextCompat;

import com.xjh.him.R;

import java.io.Serializable;

import xjh.him.activity.ChatActivity;
import xjh.him.activity.HomeActivity;
import xjh.him.activity.HomeFriendActivity;
import xjh.him.activity.HomeMeActivity;
import xjh.him.activity.HomeMsgActivity;
import xjh.him.activity.SearchActivity;
import xjh.him.common.SharedData;
import xjh.him.netty.codec.Packet;
import xjh.him.netty.dto.UserDto;


public class HomeContentFragment extends Fragment implements Serializable {
    private int resId;
    private Toolbar toolbar;
    private TextView toolbarTitle;

    public HomeContentFragment() {}

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view;
        Drawable drawable;
        switch (resId) {
            case R.id.tab_msg:
                view = inflater.inflate(R.layout.activity_home_msg, container, false);
                toolbar = view.findViewById(R.id.home_toolbar);
                toolbarTitle = view.findViewById(R.id.toolbar_title);
                drawable = ContextCompat.getDrawable(getActivity().getApplicationContext(), R.drawable.icon_plus32);
                toolbar.setOverflowIcon(drawable);
                toolbar.inflateMenu(R.menu.home_menu);
                setOnMenuItemClickListener();
                toolbarTitle.setText("消息");

                HomeMsgActivity hma = new HomeMsgActivity();
                hma.setAdapter(view);
                SharedData.msgListAdapter = hma.getAdapter();
                hma.getMsgList().setOnItemClickListener((adapterView, view1, i, l) -> {
                    Intent chatIntent = new Intent(getActivity(), ChatActivity.class);
                    UserDto.SelectByUserIdReq req = new UserDto.SelectByUserIdReq();
                    req.setUserId(hma.getAdapter().getBeans().get(i).getChat().getChatterId());
                    SharedData.client.sendMsg(Packet.create(UserDto.SelectByUserIdReq.class, req));
                    Object obj = SharedData.future.get();
                    if (obj instanceof UserDto.SelectByUserIdResp) {
                        UserDto.SelectByUserIdResp resp = (UserDto.SelectByUserIdResp) obj;
                        chatIntent.putExtra("chatter", resp.getUser());
                    }
                    chatIntent.putExtra("chat_id", hma.getAdapter().getBeans().get(i).getChat().getId());
                    startActivityForResult(chatIntent, SharedData.REQ_CODE_HOME_MSG);
                });
                break;
            case R.id.tab_friend:
                view = inflater.inflate(R.layout.activity_home_friend, container, false);
                toolbar = view.findViewById(R.id.home_toolbar);
                toolbarTitle = view.findViewById(R.id.toolbar_title);
                drawable = ContextCompat.getDrawable(getActivity().getApplicationContext(), R.drawable.icon_plus32);
                toolbar.setOverflowIcon(drawable);
                toolbar.inflateMenu(R.menu.home_menu);
                setOnMenuItemClickListener();
                toolbarTitle.setText("通讯录");

                HomeFriendActivity.initView(view, getActivity());
                SharedData.friendAdapter = HomeFriendActivity.getAdapter();
                break;
            case R.id.tab_discover:
                view = inflater.inflate(R.layout.activity_home_discover, container, false);
                toolbar = view.findViewById(R.id.home_toolbar);
                toolbarTitle = view.findViewById(R.id.toolbar_title);
                drawable = ContextCompat.getDrawable(getActivity().getApplicationContext(), R.drawable.icon_plus32);
                toolbar.setOverflowIcon(drawable);
                toolbar.inflateMenu(R.menu.home_menu);
                setOnMenuItemClickListener();
                toolbarTitle.setText("发现");
                break;
            case R.id.tab_me:
                view = inflater.inflate(R.layout.activity_home_me, container, false);
                HomeMeActivity ma = new HomeMeActivity();
                ma.init(view, getActivity());
                break;
            default:
                throw new IllegalStateException("Fragment view not initialize");
        }
        return view;
    }

    private void setOnMenuItemClickListener() {
        if (toolbar != null) {
            toolbar.setOnMenuItemClickListener((item) -> {
                //ToastUtil.show(getActivity().getApplicationContext(), item.getTitle().toString());
                int itemId = item.getItemId();
                switch (itemId) {
                    case R.id.menu_create_group_chat:
                        break;
                    case R.id.menu_apply_for_friend:
                        Intent intent = new Intent(getActivity(), SearchActivity.class);
                        getActivity().startActivity(intent);
                        break;
                }
                return true;
            });
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if ((requestCode == SharedData.REQ_CODE_USER_INFO ||
                requestCode == SharedData.REQ_CODE_HOME_MSG) &&
                resultCode == SharedData.RESULT_CODE_CHAT) {
            HomeActivity homeActivity = (HomeActivity) getActivity();
            homeActivity.showFragment(R.id.tab_msg);
        }
    }

    public void setResId(int resId) {
        this.resId = resId;
    }
}
